package com.example.controller;

import com.example.pojo.PageBean;
import com.example.pojo.Plan;
import com.example.pojo.Result;
import com.example.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/plan")
public class PlanController {
    @Autowired
    private PlanService planService;

    @GetMapping
    public Result selectAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
                            String planName, @RequestParam(defaultValue = "0") int departmentID,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        PageBean pageBean = planService.page(page, pageSize, planName, departmentID, begin, end);
        return Result.success(pageBean);
    }

    @GetMapping("/se")
    public Result result(){
        return Result.success(planService.getAll());
    }
    @PostMapping
    public Result add(@RequestBody Plan plan) {
        planService.add(plan);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result deletes(@PathVariable List<Integer> ids) {
        planService.delete(ids);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Plan plan) {
        planService.update(plan);
        return Result.success();
    }
}
